package org.fast.develop.logicflow.runtime;

import org.fast.develop.logicflow.core.meta.Definitions;
import org.fast.develop.logicflow.core.meta.shape.LogicFlowDefinition;
import org.fast.develop.logicflow.core.meta.base.Resource;
import org.fast.develop.logicflow.core.meta.shape.Start;
import org.fast.develop.logicflow.runtime.node.LogicFlowNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-11-24 12:19
 * @desc：
 */
public class LogicFlowInstanceParser {

    private Map<String, LogicFlowNode> cacheLogicFLowNodes = new HashMap<>();

    private Map<String, Resource> elements = new HashMap<>();

    private Start startElement;

    private Definitions definitions;

    public LogicFlowInstanceParser(LogicFlowDefinition logicFlowDefinition) {
        this.definitions = logicFlowDefinition.getDefinitions();
        initDefinitions(definitions);
    }

    private void initDefinitions(Definitions definitions){
        definitions.getList().forEach(element -> {
            elements.put(element.getResourceId(), element);
            if(element instanceof Start){
                startElement = (Start) element;
            }
        });
    }

    public LogicFlowNode parse(){
        LogicFlowNode logicFlowNode = getStartLogicFlowNode();
        return logicFlowNode;
    }

    public LogicFlowNode getStartLogicFlowNode(){
        LogicFlowNodeParser logicFlowNodeParser = new LogicFlowNodeParser(this);
        LogicFlowNode startLogicFlowNode = logicFlowNodeParser.getLogicFlowNode(startElement);
        return startLogicFlowNode;
    }

    public Map<String, LogicFlowNode> getCacheLogicFLowNodes() {
        return cacheLogicFLowNodes;
    }

    public Map<String, Resource> getElements() {
        return elements;
    }
}
